local StringPool = {}
StringPool.__index = StringPool

Tubes.StringPool = Tubes.MakeConstructor (StringPool)

function StringPool:ctor (size)
	self.Size = size
	self.Strings = {}
end

function StringPool:AddString (string)
	local Index = util.CRC (string) % self.Size
	if self.Strings [Index] and
		self.Strings [Index] ~= string then
		ErrorNoHalt ("String pool: Collision with \"" .. string .. "\" and \"" .. self.Strings [Index] .. "\".\n")
	end
	self.Strings [Index] = string
	self.Strings [string] = Index
	return Index
end

function StringPool:ComputeIndex (string)
	ErrorNoHalt ("String pool: Index of " .. string .. " is " .. tostring (util.CRC (string) % self.Size) .. ".\n")
	return util.CRC (string) % self.Size
end

function StringPool:LookupString (index)
	if not self.Strings [index] then
		ErrorNoHalt ("String pool: Index " .. tostring (index) .. " not found.\n")
	end
	return self.Strings [index]
end

function StringPool:LookupStringIndex (string, add)
	if add and
		not self.Strings [string] then
		return self:AddString (string)
	end
	return self.Strings [string]
end

function StringPool:RemoveString (string)
	local Index = util.CRC (string) % self.Size
	self.Strings [Index] = nil
	self.Strings [string] = nil
end